/**
 * 
 */
package de.cbf.cam.dsa.model.base;

import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.annotations.CollectionOfElements;

import de.cbf.cam.dsa.model.persistence.Header;

/**
 * @author Nebelritter
 *
 */
@Entity
public class TalentTmpl extends Header{
	public static final int TYPE_Talent				= 1<<1;
	public static final int TYPE_WeaponTalent		= 1<<2;
	public static final int TYPE_Talent_Body		= 1<<3;
	public static final int TYPE_Talent_Society 	= 1<<4;
	public static final int TYPE_Talent_Nature 		= 1<<5;
	public static final int TYPE_Talent_Knowledge 	= 1<<6;
	public static final int TYPE_Talent_Handcraft 	= 1<<7;
	public static final int TYPE_MetaTalent			= 1<<13;
	
	/**
	 * @uml.property  name="name"
	 */
	protected String name;	
	/**
	 * @uml.property  name="talentType"
	 */
	protected int talentType;
	/**
	 * @uml.property  name="sktColumn"
	 */
	protected char sktColumn;
	/**
	 * @uml.property  name="attributesToTest"
	 */
	protected String[] attributesToTest = new String[3];	
	/**
	 * @uml.property  name="compensateTalents"
	 * @uml.associationEnd  qualifier="talentName:java.lang.String java.lang.Integer"
	 */
	protected HashMap<String, Integer> compensateTalents = new HashMap<String, Integer>();
	/**
	 * @uml.property  name="beMod"
	 */
	protected float beMod;
	
	
	public static String PROPERTYNAME = "TalentTmpl:";	
	/**
	 * @uml.property  name="propertyChangeSupport"
	 */
	public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	
	
	/**
	 * 
	 */
	public TalentTmpl(String name,int type) {
		this.name = name;
		this.talentType = type;
	}
	/**
	 * 
	 */
	public TalentTmpl(TalentTmpl template) {
		this.name				= template.name;
		this.talentType			= template.talentType;
		this.attributesToTest	= template.attributesToTest;
		this.compensateTalents	= template.compensateTalents;
		this.beMod 				= template.beMod;
		this.sktColumn			= template.sktColumn;
	}
	
	//========================
// 	standard methods
//========================
	@Override
	public String toString() {
		return name;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(attributesToTest);		
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + talentType;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TalentTmpl other = (TalentTmpl) obj;
		if (!Arrays.equals(attributesToTest, other.attributesToTest))
			return false;		
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (talentType != other.talentType)
			return false;
		return true;
	}
//========================
// 	SHORT property access
//========================
	@Transient
	public List<String> getCompensateTalentsList() {
		List<String> resultList = new ArrayList<String>();
		resultList.addAll(compensateTalents.keySet());
		return resultList;
	}
	@Transient
	public int getCompensateValueForTalent(String talentName){
		return compensateTalents.get(talentName);
	}
	/**
	 * add a compensating talent <br>
	 * value is to be negative for most(its harder), but is not checked to be <0
	 * @param talentName
	 * @param value
	 */
	public void addCompensateTalent(String talentName,int value){
		compensateTalents.put(talentName,value);
	}
//========================
//		getters/setters
//========================
	@Override
	@Id
	public String getGuid() {		
		return guid;
	}
	@Override
	protected void setGuid(String guid) {
		this.guid = guid;		
	}
	/**
	 * @return
	 * @uml.property  name="name"
	 */
	public String getName() {
		return name;		
	}
	/**
	 * @param name
	 * @uml.property  name="name"
	 */
	protected void setName(String name) {
		this.name = name;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"name",null, name);
	}
	
	/**
	 * @return
	 * @uml.property  name="attributesToTest"
	 */
	public String[] getAttributesToTest() {
		return attributesToTest;
	}
	/**
	 * @param attributesToTest
	 * @uml.property  name="attributesToTest"
	 */
	public void setAttributesToTest(String[] attributesToTest) {
		this.attributesToTest = attributesToTest;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"attributesToTest",null, attributesToTest);
	}
	/**
	 * @return
	 * @uml.property  name="talentType"
	 */
	public int getTalentType() {
		return talentType;
	}
	/**
	 * @param talentType the talentType to set
	 */
	protected void setType(int talentType) {
		this.talentType = talentType;
	}	

	
	/**
	 * @return the compensateTalents
	 */
	@CollectionOfElements
	public Map<String, Integer> getCompTal() {
		return compensateTalents;
	}
	/**
	 * @param compensateTalents the compensateTalents to set
	 */
	protected void setCompTal(HashMap<String, Integer> compensateTalents) {
		this.compensateTalents = compensateTalents;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"compensateTalents",null, compensateTalents);
	}
	/**
	 * @return
	 * @uml.property  name="beMod"
	 */
	public float getBeMod() {
		return beMod;
	}
	/**
	 * @param beMod
	 * @uml.property  name="beMod"
	 */
	public void setBeMod(float beMod) {
		this.beMod = beMod;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"beMod",null, beMod);
	}
	/**
	 * @return  the sktColumn
	 * @uml.property  name="sktColumn"
	 */
	public char getSktColumn() {
		return sktColumn;
	}
	/**
	 * @param sktColumn  the sktColumn to set
	 * @uml.property  name="sktColumn"
	 */
	public void setSktColumn(char sktColumn) {
		this.sktColumn = sktColumn;
	}
	
	
}
